package br.hm.operandroid;

import java.text.ParseException;

import android.os.Parcel;
import android.os.Parcelable;
import br.hm.telephoneoperator.Phone;
import br.hm.util.PhoneUtils;

public class ContactPhone implements Parcelable {
	public int phoneKey;
	public String name;
	public Phone phone;
	public String label;
	public int labelType;

	public ContactPhone() {
	}

	public ContactPhone(Parcel in) {
		readToParcel(in);
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel parcel, int arg1) {
		parcel.writeInt(phoneKey);
		parcel.writeString(name);
		if (phone != null) {
			parcel.writeByte((byte) 1);
			parcel.writeString(phone.getCountry());
			parcel.writeString(phone.getDDD());
			parcel.writeString(phone.getNumber());
		} else {
			parcel.writeByte((byte) 0);
		}
		parcel.writeString(label);
		parcel.writeInt(labelType);
	}

	public void readToParcel(Parcel parcel) {
		phoneKey = parcel.readInt();
		name = parcel.readString();
		if (parcel.readByte() == 1) {
			String country = parcel.readString();
			String ddd = parcel.readString();
			String number = parcel.readString();
			try {
				phone = PhoneUtils.parser(number, ddd, country);
			} catch (ParseException e) {
			}
		}
		label = parcel.readString();
		labelType = parcel.readInt();
	}

	public static final Parcelable.Creator<ContactPhone> CREATOR = new Parcelable.Creator<ContactPhone>() {
		public ContactPhone createFromParcel(Parcel in) {
			return new ContactPhone(in);
		}

		public ContactPhone[] newArray(int size) {
			return new ContactPhone[size];
		}
	};
}